package com.vf.admin.common;

import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.fiwork.dto.UserInfoDto;
import com.vf.admin.fiwork.util.SpringBeanUtilsUtil;
import com.vf.admin.miaodong.entity.AppUser;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import static com.vf.admin.common.Constants.code_return_sucess;

public class BaseController {
    @Autowired
    public HttpServletRequest request;

    @Autowired
    public HttpServletResponse response;

    public Result result = new Result();

    /************提示语**********/
    public String SUCCESS_MES = "操作完成!";
    public String ERROR_MES = "操作失败!";


    /**********分页********/
    public SplitPageBean splitPage; // 分页对象
    public Integer pageNo;//当前分页页码
    private Integer pagesize = 10;        // 每页记录个数
    private Integer count = 0;    // 总记录数

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPagesize() {
        return pagesize;
    }

    public void setPagesize(Integer pagesize) {
        this.pagesize = pagesize;
    }

    /**
     * 设置分页
     *
     * @return
     */
    public void getSplitPageBean() {
        int pageIndex = (pageNo == null || pageNo.intValue() == 0) ? 1 : pageNo;
        this.setPageNo(pageIndex);
        // 2.创建分页对象，初始化 当前页，每页记录数默认为20；
        splitPage = new SplitPageBean(pageIndex);
    }

    /**
     * 设置分页
     *
     * @return
     */
    public void getSplitPageBeanpagesize(Integer pageNo) {
        int pageIndex = (pageNo == null || pageNo.equals("")) ? 1 : pageNo;
        this.setPageNo(pageIndex);
        // 2.创建分页对象，初始化 当前页，每页记录数默认为20
        splitPage = new SplitPageBean(pageIndex, pagesize);
    }

    /**
     * 设置分页
     *
     * @return
     */
    public void getSplitPageBeanpagesize(Integer pageSize, Integer pageNo) {
        Integer pageIndex = (pageNo == null || pageNo.intValue() == 0) ? 1 : pageNo;
        this.setPageNo(pageIndex);
        // 2.创建分页对象，初始化 当前页，每页记录数默认为20；
        pageSize = (pageSize == null || pageSize.intValue() == 0) ? 20 : pageSize;

        splitPage = new SplitPageBean(pageIndex, pageSize);
    }

    /**
     * 设置分页
     *
     * @return
     */
    public void getSplitPageBean(Integer pageNo) {
        Integer pageIndex = (pageNo == null || pageNo.intValue() == 0) ? 1 : pageNo;
        this.setPageNo(pageIndex);
        // 2.创建分页对象，初始化 当前页，每页记录数默认为20；
        splitPage = new SplitPageBean(pageIndex);

    }


    /**********返回字符串********/
    public String SUCCESS = "/web/common/success";
    public String ERROR = "/web/common/error";

    public String sucess(Object data, String url) {
        request.setAttribute("url", url);
        request.setAttribute("data", data);
        return url;
    }

    public Result sucessJSON(Object data) {
        Result result = new Result();
        result.setCode(code_return_sucess);
        result.setData(data);
        return result;
    }

    public Result sucessJSON(String msg) {
        Result result = new Result();
        result.setCode(code_return_sucess);
        result.setMsg(msg);
        return result;
    }


    public Result sucessJSONPay(String msg) {
        Result result = new Result();
        result.setCode(code_return_sucess);
        result.setMsg(msg);
        result.getData();
        return result;
    }


    public Result errorLogin(String msg) {
        Result result = new Result();
        result.setCode(Constants.code_login_error);
        result.setMsg(msg);
        return result;
    }

    public Result errorJSON(String msg) {
        Result result = new Result();
        result.setCode(Constants.code_return_error);
        result.setMsg(msg);
        return result;
    }

    public Result sucessJSON(Object data, String msg) {
        Result result = new Result();
        result.setCode(code_return_sucess);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }


    public Result errorUploadJSON(String msg) {
        Result result = new Result();
        result.setCode(Constants.code_upload_error);
        result.setMessage(msg);
        return result;
    }

    public Result sucessUploadJSON(String url) {
        Result result = new Result();
        result.setCode(Constants.code_upload_sucess);
        result.setUrl(url);
        return result;
    }

    public String sucess(String msg, Object data, String url) {
        request.setAttribute("msg", msg);
        request.setAttribute("url", url);
        request.setAttribute("data", data);
        return url;
    }

    public Result sucess(String msg, Object data, SplitPageBean splitPage) {
        Result result = new Result();
        result.setCode(code_return_sucess);
        result.setMsg(msg);
        result.setData(data);
        if (splitPage != null && splitPage.getTotalCount() > 0) {
            result.setCount(splitPage.getTotalCount());
        } else {
            result.setCount(0);
        }
        return result;
    }


    public String error(String msg, String url) {
        request.setAttribute("msg", msg);
        request.setAttribute("url", url);
        return url;
    }

    public String error(Object data, String url) {
        request.setAttribute("url", url);
        request.setAttribute("data", data);
        return url;
    }

    public String error(String msg, Object data, String url) {
        request.setAttribute("msg", msg);
        request.setAttribute("url", url);
        request.setAttribute("data", data);
        return url;
    }

    public String error(String msg) {
        request.setAttribute("msg", msg);
        return ERROR;
    }

    public Result sucess(Object data) {
        Result result = new Result();
        result.setCode(code_return_sucess);
        result.setData(data);
        return result;
    }

    public Result sucess(Object data, SplitPageBean splitPage) {
        Result result = new Result();
        result.setCode(code_return_sucess);
        result.setData(data);
        if (splitPage != null && splitPage.getTotalCount() > 0) {
            result.setCount(splitPage.getTotalCount());
        } else {
            result.setCount(0);
        }
        return result;
    }

    public String sucess(String url) {
        return url;
    }

    public Result sucess() {
        Result result = new Result();
        result.setCode(code_return_sucess);
        result.setMsg("操作成功!");
        return result;
    }


    public Result error() {
        Result result = new Result();
        result.setCode(Constants.code_return_error);
        result.setMsg("操作异常，请联系管理员！");
        return result;
    }

    /**
     * 成功 分页
     **/
    public Result sucessPage(Object date, Object page) {
        Result result = new Result();
        result.setCode(code_return_sucess);
        result.setMsg("成功");
        result.setData(date);
        result.setPage(page);
        return result;
    }

    /***
     * 使用Ajax返回结果时的简化处理
     * @param data
     * @throws IOException
     */
    private void toJSON(Object data) throws IOException {
        this.response.setCharacterEncoding("utf-8"); //用utf-8转换获得传输过来的码
        this.response.setContentType("application/json");
        PrintWriter out = this.response.getWriter();
        out.print(data);
        out.flush();
        out.close();
    }


    public Result result(int code, String msg, Object data) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public Result result(int code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public void setRefresh(boolean refresh) {
        request.setAttribute("isParendRefresh", refresh); // 设置父页面是否刷新(用户弹出层编辑页面更新操作时，刷新父窗口)
    }


    /**
     * 过滤用户信息的实体
     *
     * @param user
     * @return
     */
    public UserInfoDto userInfo(AppUser user) {
        UserInfoDto userInfoDto = new UserInfoDto();
        SpringBeanUtilsUtil.copyPropertiesIgnoreNull(user, userInfoDto);
        return userInfoDto;
    }

    /**
     * 设置跳转路径
     *
     * @return
     */
    public void setUrl(String url) {
        request.setAttribute("URL", url);
        Object urlObj = request.getAttribute("URL");
        if (urlObj == null) {
            request.setAttribute("isParendRefresh", false);
        }
    }

    /**
     * 设置提示语
     *
     * @return
     */
    public void setMessage(Object message) {
        System.out.println(message);
        request.setAttribute("message", message);
    }

//    public Integer getPageNo() {
//        return pageNo;
//    }
//
//    public void setPageNo(Integer pageNo) {
//        this.pageNo = pageNo;
//    }
//
//    public SplitPageBean getSplitPage() {
//        return splitPage;
//    }
//
//    public void setSplitPage(SplitPageBean splitPage) {
//        this.splitPage = splitPage;
//    }
//
//    public Integer getCount() {
//        return count;
//    }
//
//    public void setCount(Integer count) {
//        this.count = count;
//    }
//
//    public Integer getPagesize() {
//        return pagesize;
//    }
//
//    public void setPagesize(Integer pagesize) {
//        this.pagesize = pagesize;
//    }
}
